library Airstrike initializer init requires LegacySystem

	private function AirstrikeDamageLoop takes nothing returns nothing
	   local timer t = GetExpiredTimer() 
	   local unit source = getUnit(t,"source")
	   local real x = getReal(t,"x")
	   local real y = getReal(t,"y")
	   local integer dmg = getInteger(t,"damage_per_tick")
	   local integer left = getInteger(t,"ticks_left")
	   set left = left -1
	   if( left<0 ) then 
		  set t = null
		  set source = null
		  call FlushHandleLocals(t)
		  call ReleaseTimer(t)
		  return
	   endif
	   
	   call putInteger(t,"ticks_left", left)
	   call DamageAreaExplosion(source,x,y,dmg,400.00)
	   set t = null
	   set source = null
	endfunction


	private function AirstrikeDamage takes real x, real y, unit u returns nothing
		local timer t = NewTimer()
		local rect r 
		local group g = CreateGroup()
		call putReal(t,"x",x)
		call putReal(t,"y",y)
		call putInteger(t,"damage_per_tick", 1400)
		call putInteger(t,"ticks_left", 6)
		call putUnit(t,"source",u)
			
		set r = Rect(x-740.0,y-740.0,x+740.0,y+740.0)
		call DamageTrees(r,10000)

		call DamageAreaExplosion(u,x,y, 4000, 400.00)
		call TimerStart(t,0.25,false, function AirstrikeDamageLoop)
		
		call DestroyGroup(g)
		call RemoveRect(r)
		set t = null
		set r = null
		set g = null
	endfunction

	private function AirstrikeKaboom takes player p, real x, real y returns nothing
		local unit u 
		set u = CreateUnit(p,'e00A',x,y,0.0)
		call UnitApplyTimedLife(u,'Bhwd',1.5)
		call DestroyEffect(AddSpecialEffect( "Effect_FireRing.mdx",x,y))
		set u = CreateUnit(p,'e004',x,y,0.0)
		call UnitApplyTimedLife(u,'Bhwd',7.5)
		set u = null
	endfunction 
	
	private function AirstrikeInbound takes nothing returns nothing
		local integer i = 1
		local unit u
		local trigger t = GetTriggeringTrigger()
		local unit u1 = getUnit(t,"u1")
		local unit u2 = getUnit(t,"u2")
		local unit u3 = getUnit(t,"u3")
		local player p = getPlayer(t,"p")
		local location exitloc = getLocation(t,"exitloc")
		local location target = getLocation(t,"target")
		local real flighttime = getReal(t,"flighttime")
		local real xt = GetLocationX(target)
		local real yt = GetLocationY(target)
		local real dx = GetUnitX(u1) - xt
		local real dy = GetUnitY(u1) - yt
		local real dx2 = GetUnitX(u2) - xt
		local real dy2 = GetUnitY(u2) - yt
		local group missiles
		local boolean docleanup = false
	
		call IssuePointOrderLoc(u1, "move", exitloc)
		call IssuePointOrder(u2, "move", GetLocationX(exitloc)+150.0, GetLocationY(exitloc)-150.0)
		call IssuePointOrder(u3, "move", GetLocationX(exitloc)-150.0, GetLocationY(exitloc)-150.0)
		if dx*dx+dy*dy<=4000000.0 or dx2*dx2+dy2*dy2<=4000000.0 then //2000 radius
			call DisableTrigger(t)
			call SetForceAllianceStateBJ(bj_FORCE_PLAYER[9],udg_UnknownEntities,bj_ALLIANCE_ALLIED)
			if udg_Mode!=3 and GetRandomInt(0,2)==1 then
				call GeneralText(5.0, "|cfff3ad00F-22 Pilot|r : Missiles fired." )
			endif
			set missiles = CreateGroup()
			set i = 1
			set udg_soundpath = "Abilities\\Spells\\Other\\TinkerRocket\\TinkerMissileLaunch1.wav"
			call PlaySoundPath.execute()
			loop
				exitwhen i>3
				set u = CreateUnit( p, 'h00C', GetUnitX(getUnit(t,"u"+I2S(i)))+8.0, GetUnitY(getUnit(t,"u"+I2S(i)))+8.0 ,0 )
				call IssuePointOrder(u,"attackground",xt,yt)
				call GroupAddUnit(missiles,u)
				set u = CreateUnit( p, 'h00C', GetUnitX(getUnit(t,"u"+I2S(i)))-8.0, GetUnitY(getUnit(t,"u"+I2S(i)))-8.0 ,0 )
				call IssuePointOrder(u,"attackground",xt,yt)
				call GroupAddUnit(missiles,u)
				if GetRandomInt(0,1)==1 then
					call TriggerSleepAction(0.)
				endif
				set i=i+1
			endloop
			set u = null
			set udg_soundpath = "Abilities\\Spells\\Other\\TinkerRocket\\TinkerMissileLaunch1.wav"
			call PlaySoundPath.execute()
			call DestroyEffect(AddSpecialEffect( "Objects\\Spawnmodels\\Other\\NeutralBuildingExplosion\\NeutralBuildingExplosion.mdl", xt, yt ))
			
			call TriggerSleepAction(0.1)
			// do damage
			if udg_Mode==3 then
				call ArtHandler(udg_Hero[GetConvertedPlayerId(p)],xt,yt,10, 3000.0,420.0,0.1,2)
			else
				call ArtHandler(u1,xt,yt,10,3000.0,420.0,0.1,2)
			endif
			call TriggerSleepAction(0.5)
			call AirstrikeDamage(xt, yt, u1)
			call AirstrikeKaboom(p, xt,yt)
			
			call SetUnitFlyHeight(u1,1200.0,200.0)
			call SetUnitFlyHeight(u2,1200.0,200.0)
			call SetUnitFlyHeight(u3,1200.0,200.0)
			call PolledWait(0.1)
			call EarShockArea(xt,yt,500)
			call ForGroup(missiles,function RemoveUnitAll)
			call PolledWait(0.1)
			set udg_myinteger = 5
			call EnumDestructablesInCircleBJ(500,target,function killtree)
			call SetForceAllianceStateBJ(bj_FORCE_PLAYER[9],udg_UnknownEntities,bj_ALLIANCE_UNALLIED)
			call DestroyGroup(missiles)
			set udg_soundpath = "Sound\\Interface\\GlueScreenMeteorLaunch3.wav"
			call PlaySoundPath.execute()
			call PolledWait(6.)
			set docleanup = true
		else
			set flighttime=flighttime+0.25
			call putReal(t,"flighttime",flighttime)
		endif
		if flighttime>30.0 or docleanup then
			call RemoveUnit(u1)
			call RemoveUnit(u2)
			call RemoveUnit(u3)
			call RemoveLocation(target)
			call RemoveLocation(exitloc)
			if udg_Mode!=3 then
				call RemoveUnit( getUnit(t,"beacon") )
			endif
			call DisableTrigger(t)
			set missiles = null
			set t = null
			set u1 = null
			set u2 = null
			set u3 = null
			set exitloc = null
			set target = null
			//call TriggerRemoveAction(GetTriggeringTrigger(),GetHandleTriggerAction(GetTriggeringTrigger(),"trigac"))
		endif
		set missiles = null
		set t = null
		set u1 = null
		set u2 = null
		set u3 = null
		set exitloc = null
		set target = null
	endfunction

	private function AirstrikeConditions takes nothing returns boolean
		local integer i = GetSpellAbilityId()
		return i=='A04J' or i=='A05H' or i == 'A097'
	endfunction

	private function Airstrike takes nothing returns nothing
		local integer i = 1
		local location l = GetSpellTargetLoc()
		local real x1 = GetLocationX(l)
		local real y1 = GetLocationY(l)
		local real dx
		local real dy
		local real len
		local trigger t
		local player p = GetOwningPlayer(GetTriggerUnit())
		local unit u
		local real r = GetRandomReal(2800.0,5000.0)
		local real theta = GetRandomReal(0.0,6.28)
		local real ux = Clamp(x1+r*Cos(theta),GetRectMinX(bj_mapInitialPlayableArea),GetRectMaxX(bj_mapInitialPlayableArea))
		local real uy = Clamp(y1+r*Sin(theta),GetRectMinY(bj_mapInitialPlayableArea),GetRectMaxY(bj_mapInitialPlayableArea))
		local unit raptor1 = CreateUnit(Player(11),'h00B',ux,uy,theta+3.14)
		local unit raptor2
		local unit raptor3
		local real x2 = GetUnitX(raptor1)
		local real y2 = GetUnitY(raptor1)

		call TriggerSleepAction(0.)
		set raptor2 = CreateUnit(Player(11),'h00B',ux,uy,theta+3.14)
		call TriggerSleepAction(0.)
		set raptor3 = CreateUnit(Player(11),'h00B',ux,uy,theta+3.14)
		call TriggerSleepAction(0.)
		call SetCameraQuickPosition(x1,y1)
		call PingMinimapEx(x1, y1, 15.0, 204, 196, 115, false)
		set u = CreateUnit(Player(14),'n00P',x1,y1,0.0) //DangerZone Dummy
		call UnitApplyTimedLife(u,'Bhwd',15.0)
		if udg_Mode!=3 then
			call ALICEText(10.0, "Airstrike inbound at coordinates ( " + I2S(R2I(x1)) + ", " + I2S(R2I(y1)) + " )." )
		endif
		
		loop
			exitwhen i > udg_Players
			call UnitShareVision(raptor1,ConvertedPlayer(udg_Sorted[i]),true)
			call UnitShareVision(raptor2,ConvertedPlayer(udg_Sorted[i]),true)
			call UnitShareVision(raptor3,ConvertedPlayer(udg_Sorted[i]),true)
			set i = i + 1
		endloop
		if udg_Mode!=3 then
			call StartSound(gg_snd_F22Pilot)
		endif
		
		set dx = x1-x2
		set dy = y1-y2
		set len = SquareRoot(dx*dx+dy*dy+1)
		set t = CreateTrigger()
		call TriggerRegisterTimerEvent(t,0.25,true)
		call putUnit(t,"u1",raptor1)
		call putUnit(t,"u2",raptor2)
		call putUnit(t,"u3",raptor3)
		call putPlayer(t,"p",p)
		call putLocation(t,"exitloc",Location(x1+1200*dx/len,y1+1200*dy/len))
		call putLocation(t,"target",l)
		call putReal(t,"flighttime", 0.0)
		if udg_Mode!=3 then
			call putUnit(t,"beacon",CreateUnit(p,'e00D',x1,y1,0.0))
		endif
		call TriggerAddAction(t,function AirstrikeInbound)

		call TriggerSleepAction(1.0)
		call PingMinimapEx(x1,y1,2.5,255,0,0,false)
		call TriggerSleepAction(1.0)	
		call PingMinimapEx(x1,y1,2.5,255,0,0,false)
		call TriggerSleepAction(1.0)			
		call PingMinimapEx(x1,y1,2.5,255,0,0,false)
		call TriggerSleepAction(1.0)						
		call PingMinimapEx(x1,y1,2.5,255,0,0,false)
		call TriggerSleepAction(1.0)								
		call PingMinimapEx(x1,y1,2.5,255,0,0,false)
		call TriggerSleepAction(1.0)						
		call PingMinimapEx(x1,y1,2.5,255,0,0,false)
		
		//call AirstrikeInbound ()
		set t = null
		set u = null
		set l = null
		set raptor1 = null
		set raptor2 = null
		set raptor3 = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
		call TriggerAddCondition(t,Condition(function AirstrikeConditions))
		call TriggerAddAction(t,function Airstrike)
	endfunction




endlibrary